package net.neptech.utils;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public final class SimpleOrderNoStr {

    private static final DateTimeFormatter FMT =
            DateTimeFormatter.ofPattern("yyMMddHHmmssSSS").withZone(ZoneId.systemDefault());

    private static volatile long lastMs = -1L;
    private static int seq = 0; // 0..9999

    private SimpleOrderNoStr() {}

    /** 无前缀：返回纯数字字符串 */
    public static String next() {
        return next("");
    }

    /** 自定义前缀（可空），返回字符串 */
    public static synchronized String next(String prefix) {
        long now = System.currentTimeMillis();

        if (now != lastMs) {            // 新毫秒，序列归零
            lastMs = now;
            seq = 0;
        } else if (seq >= 9999) {       // 当前毫秒序列用尽，等到下一毫秒
            do { now = System.currentTimeMillis(); } while (now == lastMs);
            lastMs = now;
            seq = 0;
        }

        String timePart = FMT.format(Instant.ofEpochMilli(lastMs));
        String s4 = String.format("%04d", seq++);

        String pre = (prefix == null) ? "" : prefix;
        return pre + timePart + s4;
    }

    // 示例

}
